﻿using ClosedXML.Excel;
using FinScreen.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using FinScreen.Util;

namespace FinScreen
{
    class MasterBase : Observable<Instrument>
    {
        public void Load()
        {
            LoadSingle(@"ss.a.xlsx",".ss");
            LoadSingle(@"sz.a.xlsx",".sz");

            OnComplete();
        }

        private void LoadSingle(string name, string exchange)
        {
            string path = System.IO.Path.Combine(Thread.GetDomain().BaseDirectory, name);
            using (var workbook = new XLWorkbook(path))
            {
                var sheet = workbook.Worksheet(1);
                foreach (var row in sheet.Rows())
                {
                    var code = row.Cell("A").Value.ToString();
                    if (code == "代码" || string.IsNullOrEmpty(code)) continue;

                    Instrument instrument = new Instrument()
                    {
                        Code = code + exchange,
                        Name = row.Cell("B").Value.ToString(),
                        Industry = row.Cell("S").Value.ToString(),
                        PEG = row.Cell("P").Value.ToString().ToDouble()
                    };
                    OnNext(instrument);
                }
            }
        }
    }
}